// 221.最大正方形
package Leetcode_Second_Brush;

public class Solution221 {
    public int maximalSquare(char[][] matrix) {
        int row = matrix.length;
        int column = matrix[0].length;

        int[][] dp = new int[row][column];

        int maxSide = -1;

        for (int i = 0; i < row; i++)
            for (int j = 0; j < column; j++) {
                dp[i][j] = matrix[i][j] == '1' ? 1 : 0;
                maxSide = Math.max(maxSide, dp[i][j]);
            }

        for (int i = 1; i < row; i++)
            for (int j = 1; j < column; j++) {
                if (matrix[i][j] == '0')
                    dp[i][j] = 0;
                else {
                    dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
                }
                maxSide = Math.max(maxSide, dp[i][j]);
            }

        return maxSide * maxSide;
    }
}
